<?php


namespace Xiaoguai\DesignPattern\DP023\Expression;


use Xiaoguai\DesignPattern\DP023\ExpressionInterface;

class AndExpression implements ExpressionInterface
{
    /**
     * @var TerminalExpression
     */
    protected $cityExpression;

    /**
     * @var TerminalExpression
     */
    protected $personExpression;

    /**
     * and表达式需要两个参数(terminal_expression)
     * AndExpression constructor.
     * @param TerminalExpression $cityExpression
     * @param TerminalExpression $personExpression
     */
    public function __construct(TerminalExpression $cityExpression, TerminalExpression $personExpression)
    {
        $this->cityExpression = $cityExpression;
        $this->personExpression = $personExpression;
    }

    /**
     * 开始针对传入的context来进行and计算
     * 在 [规定的城市] 且 [规定的person] 时才返回true
     * @param string $context
     * @return bool
     */
    public function interpret(string $context): bool
    {
        list($city, $person) = explode('的', $context);

        $isFreeCity = $this->cityExpression->interpret($city);
        $isFreePerson = $this->personExpression->interpret($person);
        return $isFreeCity && $isFreePerson;
    }
}